<?php

namespace app\common\service\logistics;

use app\common\library\ApiException;
use app\common\model\Address as AddressMd;
use think\Db;
use think\Exception;
use think\Request;
use think\Validate;

/**
 * 地址管理
 */
class Address
{

    /**
     * 添加收货地址
     * @param $uid
     * @param Request $request
     * @return true
     * @throws ApiException
     */
    public function Add($uid,Request $request)
    {
        $request->filter(['strip_tags']);
        $name = $request->param("name","");
        if(!$name){
            throw new ApiException("请填写姓名");
        }
        $mobile = $request->param("mobile","");
        if(!$mobile ||  !Validate::regex($mobile, "^1\d{10}$")) {
            throw new ApiException("手机格式不正确");
        }
        $province = $request->param("province");
        $city     = $request->param("city");
        $district = $request->param("district");
        if(!$province || !$city || !$district){
            throw new ApiException("请填写省市区!");
        }
        $name_address = $request->param("name_address");
        if(!$name_address){
            throw new ApiException("请填写详细地址");
        }
        $door_number  = $request->param("door_number");
        if(!$door_number){
            throw new ApiException("请填写门牌号");
        }
        $location = $request->param("location");
        if(!$location){
            throw new ApiException("经纬度获取失败");
        }
        Db::startTrans();
        try {
            $data = [
                "user_id"   => $uid,
                "name"      => $name,
                "mobile"    => $mobile,
                "is_default"=> 0,
                "province"  => $province,
                "city"      => $city,
                "district"  => $district,
                "area_address"  => $province.$city.$district,
                "name_address"  => $name_address,
                "door_number"   => $door_number,
                "location"      => $location
            ];
            $a_id = $request->param("a_id");
            $row = AddressMd::where("id",$a_id)->find();
            if($row){
                $res = $row->save($data);
                Db::commit();
                return $row->id;
            }else{
                $res = AddressMd::create($data);
                Db::commit();
                return $res->id;
            }

        }catch (Exception $e){
            Db::rollback();
            throw new ApiException($e->getMessage());
        }
    }

    /**
     * 删除地址
     * @param $id
     * @return true
     * @throws ApiException
     */
    public function del_address($id)
    {
        Db::startTrans();
        try {
            $res = AddressMd::where("id",$id)->delete();
            if(!$res){
                throw new Exception("删除失败");
            }
            Db::commit();
            return true;
        }catch (Exception $e){
            Db::rollback();
            throw new ApiException($e->getMessage());
        }
    }

    /**
     * 地址列表
     * @param $uid
     */
    public function add_list($uid)
    {
        return AddressMd::where("user_id",$uid)->order("id desc")->select();
    }

    /**
     * 根据当前位置获取当前地址簿
     * @param $uid
     * @param Request $request
     * @return array|mixed
     */
    public function address_info($uid,Request $request)
    {
        $request->filter(['strip_tags']);
        $lat = $request->param("lat");
        $lnt = $request->param("lnt");
        $address_list = $this->add_list($uid);
        $address_info = [];
        foreach($address_list as $l){
            $location = explode(",",$l["location"]);
            $range_num = distance($lat,$lnt,$location[0],$location[1]);
            if($range_num <= "0.02"){
                $address_info = $l;
                break;
            }
        }
        return $address_info;
    }
}